<template>
  <div ref="appRef" class="home">
    <div class="bigScreen">
      <img class="img" src="../assets/images/binzhou/head.png" />
      <img class="title-img" src="../assets/images/binzhou/head-title.png" />
      <div class="content">
        <div class="content-left">
          <div class="content-left-one">
            <div class="content-left-one-top">
              <quality-efficiency
                :data="pageData.improveQualityEfficiency"
                :delay="200"
              ></quality-efficiency>
            </div>
            <div class="content-left-one-bottom">
              <top-list :list="pageData.highlightRobotList"></top-list>
            </div>
          </div>
          <div class="content-left-two">
            <div class="content-left-two-top">
              <demand-management :list="pageData.requrement"></demand-management>
            </div>
            <div class="content-left-two-bottom">
              <professional-coverage :info="pageData.professionCoverage"></professional-coverage>
            </div>
          </div>
        </div>
        <div class="content-center">
          <div class="content-center-top">
            <content-center :info="pageData.digitalEmployees"></content-center>
          </div>
          <div class="content-center-bottom">
            <content-center-bottom></content-center-bottom>
          </div>
        </div>
        <div class="content-right">
          <content-right
            :inspection-robot-list="pageData.inspectionRobotList"
            :highest-efficiency-improvement-list="pageData.highestEfficiencyImprovementList"
            :robot-specialty-list="pageData.robotSpecialtyList"
            :digital-supply-station-list="pageData.digitalSupplyStationList"
          ></content-right>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { onMounted, reactive, watchEffect, ref, onUnmounted } from 'vue'
  import useIndex from '@/utils/userDraw'
  import bus from '@/utils/bus'
  import QualityEfficiency from '@/components/Binzhou/QualityEfficiency.vue'
  import TopList from '@/components/Binzhou/TopList.vue'
  import DemandManagement from '@/components/Binzhou/DemandManagement.vue'
  import ProfessionalCoverage from '@/components/Binzhou/ProfessionalCoverage.vue'
  import ContentCenter from '@/components/Binzhou/ContentCenter.vue'
  import ContentCenterBottom from '@/components/Binzhou/ContentCenterBottom.vue'
  import ContentRight from '@/components/Binzhou/content-right/contentRight.vue'
  import { getDataByBinchou } from '../api/index'
  import appStore from '@/store'
  import datas from '@/api/data/index.json'

  import type {
    ImproveQualityEfficiency,
    HighestEfficiencyImprovementList,
    Requrement,
    DigitalSupplyStationList,
    HighlightRobotList,
    ProfessionCoverage,
    InspectionRobotList,
    DigitalEmployee,
    RobotSpecialtyList
  } from '@/api/data'
  const { appRef, calcRate, windowDraw } = useIndex()
  const { setImproveQualityEfficiency } = appStore.indexStore
  const timer = ref(0)
  const areaCode = ref(0)
  const pageData = reactive({
    digitalSupplyStationList: [] as DigitalSupplyStationList[],
    robotSpecialtyList: [] as RobotSpecialtyList[],
    highestEfficiencyImprovementList: [] as HighestEfficiencyImprovementList[],
    inspectionRobotList: [] as InspectionRobotList[],
    highlightRobotList: {} as HighlightRobotList,
    requrement: {} as Requrement,
    improveQualityEfficiency: {} as ImproveQualityEfficiency,
    professionCoverage: {} as ProfessionCoverage,
    digitalEmployees: {} as DigitalEmployee
  })

  bus.on('change', (code) => {
    areaCode.value = code
    fetchData()
  })

  const GetRandomNum = (Min: number, Max: number) => {
    let Range = Max - Min
    let Rand = Math.random()
    return Min + Math.round(Rand * Range)
  }

  /**获取并处理数据 */
  const fetchData = async () => {
    try {
      let res = null
      if (import.meta.env.MODE.indexOf('development') > -1) {
        try {
          // 先访问测试接口
          res = await getDataByBinchou('/gwbzApi/bigScreenInfo', areaCode.value)
        } catch (error) {
          console.error('接口访问失败：', error)
          // 接口方式失败之后使用 假数据
          res = datas
        }
      } else {
        res = await getDataByBinchou('/gwbzApi/bigScreenInfo', areaCode.value)
      }

      pageData.improveQualityEfficiency = res.data.improveQualityEfficiency
      pageData.highlightRobotList = res.data.highlightRobotList
      pageData.requrement = res.data.requrement
      pageData.professionCoverage = res.data.professionCoverage
      pageData.digitalEmployees = res.data.digitalEmployees

      pageData.digitalSupplyStationList = res.data.digitalSupplyStationList
      pageData.highestEfficiencyImprovementList = res.data.highestEfficiencyImprovementList
      pageData.robotSpecialtyList = res.data.robotSpecialtyList
      pageData.inspectionRobotList = res.data.inspectionRobotList

      if (import.meta.env.MODE.indexOf('development') > -1) {
        // 所有测试需要模拟的操作，放置于此处
        // pageData.improveQualityEfficiency.efficiencyImproveRate = GetRandomNum(10, 100)
        // pageData.improveQualityEfficiency.accuracyImproveRate = GetRandomNum(10, 100)
      }
      setImproveQualityEfficiency(pageData.improveQualityEfficiency)
    } catch (error: any) {
      throw new Error(error.message as any)
    }
  }

  // 周期性获取
  const periodicFetch = (time: number) => {
    fetchData()
    timer.value = window.setInterval(fetchData, time)
  }

  onMounted(() => {
    periodicFetch(30000)
    windowDraw()
    calcRate()
  })

  onUnmounted(() => {
    window.clearInterval(timer.value)
  })
</script>

<style lang="scss" scoped>
  $sideWidth: 560px;
  .home {
    width: 1920px;
    height: 810px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transform-origin: left top;
    .bigScreen {
      width: 100%;
      height: 100%;
      background-image: url('@/assets/images/binzhou/sum-bg.png');
      background-size: cover;
      background-repeat: no-repeat;
      background-position: center center;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .img {
        width: 100%;
        height: 77px;
      }
      .title-img {
        position: absolute;
        width: 345px;
        height: 26px;
        left: 788px;
        top: 15px;
      }
      .content {
        display: flex;
        height: 97.8%;
        font-size: 30px;
        &-left {
          width: 800px;
          height: 100%;
          display: flex;
          &-one {
            margin: 18px 6px 18px 31px;
            width: 405px;
            height: 100%;
            &-top {
              width: 100%;
            }
            &-bottom {
              width: 100%;
            }
          }
          &-two {
            margin: 18px 0px 18px 0px;
            width: 400px;
            height: 100%;
            &-top {
              width: 100%;
              height: 324px;
            }
            &-bottom {
              width: 100%;
              height: 314px;
            }
          }
        }
        &-center {
          &-top {
            width: 327px;
            height: 150px;
          }
          &-bottom {
            width: 306px;
            height: 515px;
          }
        }
        .content-right {
          padding: 0 20px;
        }
      }
    }
  }
</style>
